﻿/*************************************************************************************
 * 工    具：  HK_SimpleQuiz
 * 
 * 描    述：  简单的考题功能，目前包含了：单选题、多选题、判断题，
 *            可通过Json或Excel文件对题库进行配置其中通过Excel配置的方式只支持PC端。
 * 
 * 版    本：  V1.1
 * 作    者：  京产肠饭
 * 
 * 创    建：  2021/11/25  V1.0
 *
 * 更    新：  2025/09/05  V1.1
 *            1.代码优化：优化代码组织结构，提高可读性、稳定性
 *            2.功能优化：答题进度刷新修改为按时间间隔刷新，兼容鼠标及触控
 * 
 * 链    接：  https://gitee.com/JingChanChangFan/hk_-unity-tools
*************************************************************************************/

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using QFramework;

namespace HKTools
{
    public interface IHK_SimpleQuizSystem : ISystem
    {
        /// <summary>
        /// 通过 Json 文件，读取题库数据，可通过URL或者路径读取
        /// </summary>
        void GetQuesDataFromJson(string filePath);
    }

    public class HK_SimpleQuizSystem : AbstractSystem, IHK_SimpleQuizSystem
    {
        protected override void OnInit()
        {

        }

        /// <summary>
        /// 通过 Json 文件，读取题库数据，可通过URL或者路径读取
        /// </summary>
        public void GetQuesDataFromJson(string filePath)
        {
            ActionKit.Sequence()
                .Coroutine(() => GetQuesData_IE(filePath, ProcessQuesData))
                .StartGlobal();
        }

        IEnumerator GetQuesData_IE(string filePath, Action<HK_QuizData> callback)
        {
            UnityWebRequest request = UnityWebRequest.Get(filePath);
            yield return request.SendWebRequest();

            Debug.Log(request.downloadHandler.text);

            HK_QuizData quizDataList = JsonUtility.FromJson<HK_QuizData>(request.downloadHandler.text);

            callback(quizDataList);
        }

        // 数据获取后进行数据存储
        void ProcessQuesData(HK_QuizData quesDataList)
        {
            IHK_SimpleQuizModel quizDataModel = this.GetModel<IHK_SimpleQuizModel>();
            quizDataModel.QuizData.Value = quesDataList;

            quizDataModel.Single_Num.Value = quesDataList.SingleChoice_List.Count;
            quizDataModel.Multiple_Num.Value = quesDataList.MultipleChoice_List.Count;
            quizDataModel.TrueFalse_Num.Value = quesDataList.TrueFalse_List.Count;
            quizDataModel.Blank_Num.Value = quesDataList.Blank_List.Count;
            quizDataModel.ShortAnswer_Num.Value = quesDataList.ShortAnswer_List.Count;

            quizDataModel.TotalScore.Value = quesDataList.SingleChoice_Score * quesDataList.SingleChoice_List.Count
                + quesDataList.MultipleChoice_Score * quesDataList.MultipleChoice_List.Count
                + quesDataList.TrueFalse_Score * quesDataList.TrueFalse_List.Count
                + quesDataList.Blank_Score * quesDataList.Blank_List.Count
                + quesDataList.ShortAnswer_Score * quesDataList.ShortAnswer_List.Count;

            Debug.Log("单选题共：" + quesDataList.SingleChoice_List.Count);
            Debug.Log("多选题共：" + quesDataList.MultipleChoice_List.Count);
            Debug.Log("判断题共：" + quesDataList.TrueFalse_List.Count);
            Debug.Log("填空题共：" + quesDataList.Blank_List.Count);
            Debug.Log("简答题共：" + quesDataList.ShortAnswer_List.Count);

            this.SendEvent<Event_InitQuesItems>();
        }
    }
}